<?php
/*
 * 汽车图片
 * @package    auto
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: bdc085073d279f2d49c093eefa1d5fa68b9f868b $
 */
defined('IN_IXDCW') or exit('Access Denied');

/**
 * 汽车图片类
 * Class AutoPictureModule
 */
class AutoPictureModule extends BasicModule {

	/** @var 模块id */
	private $_moduleid;

	/** @var 条目ID */
	private $_apid;

	/** @var string 表格 */
	private $_table;

	/** @var array 可用字段 */
	private $_fields;

	/**
	 * 构造函数
	 * @param $moduleid
	 */
	function __construct($moduleid) {
		parent::__construct();
		$this->_moduleid = $moduleid;
		$this->_fields   = array(
			'arbid',
			'armid',
			'arcid',
			'armid',
			'amid',
			'alid',
			'imgurl',
			'title',
			'level',
			'description',
			'status',
			'addtime',
			'level',
			'editor',
			'edittime',
			'listorder'
		);
	}

	public function setTable($carId) {
		$this->_table =  Table::autoPicture($carId);
	}

	public function setApid($apid) {
		$this->_apid = $apid;
	}

	public function getApid() {
		return $this->_apid;
	}

	public function getFields() {
		return $this->_fields;
	}

	/**
	 * 是否通过插入检测
	 * @param $post
	 * @return bool
	 */
	function pass($post) {
		if (!is_array($post)) return false;
		if (!$post['arbid']) return $this->err('请选择品牌');
		if (!$post['arcid']) return $this->err('请选择车系');
		if (strlen($post['title']) < 3) return $this->err('请填写标题');
		return true;
	}

	/**
	 * 设置符合内容的值
	 * @param $post
	 * @return array|mixed
	 */
	function set($post) {
		global $DT_TIME, $DT_IP, $_username, $_userid;
		$thumbSeName         = $this->_moduleid . '_imgurl';
		$post['addtime']     = (isset($post['addtime']) && $post['addtime']) ? strtotime($post['addtime']) : $DT_TIME;
		$post['edittime']    = $DT_TIME;
		$post['amid']        = intval($post['amid']);
		$post['title']       = trim($post['title']);
		$post['description'] = trim($post['description']);
		if ($this->_apid) {
			// 修改信息
			$r              = $this->getOne();
			$post['editor'] = $_username;
			ThumbUpload::clearPicture($thumbSeName, $post['imgurl'], $r['imgurl'], $_userid);
		} else {
			$post['username'] = $post['editor'] = $_username;
			$post['ip']       = $DT_IP;
			ThumbUpload::clearPicture($thumbSeName, $post['thumb'], '', $_userid);
		}
		return $post;
	}

	/**
	 * 获取一条数据
	 * @return array|void
	 */
	function getOne() {

		return $this->_db->getOne("SELECT * FROM {$this->_table} WHERE apid=$this->_apid");



	}

	/**
	 * 获取列表
	 * @param string $condition
	 * @param string $order
	 * @param string $cache
	 * @return array
	 */
	function getList($condition = 'status=3', $order = 'addtime DESC', $cache = '') {
		global $items;
		$num   = $this->_db->count($this->_table, $condition, $cache);
		$items = $num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists  = $catids = $CATS = array();
		$result = $this->_db->query($sql, $cache);
		while ($r = $this->_db->fetchArray($result)) {
			$r['edittime']       = timetodate($r['edittime'], 4);
			$r['alt']            = $r['title'];
			$r['title']          = set_style($r['title'], $r['style']);
			$catids[$r['catid']] = $r['catid'];
			$lists[]             = $r;
		}
		return $lists;
	}

	/**
	 * @param $post
	 * @return int|string
	 */
	function add($post) {
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		return $this->_db->insertId();
	}

	/**
	 * 编辑
	 * @param $post
	 * @return bool
	 */
	function edit($post) {
		$post = $this->set($post);
		$sql  = Sql::update($this->_table, $post, "apid={$this->_apid}", '', $this->_fields);
		$this->_db->query($sql);
		return true;
	}

	/**
	 * 更新
	 * @param        $apid
	 * @param array  $item
	 */
	function update($apid, $item = array()) {
		$item or $item = $this->_db->getOne("SELECT * FROM {$this->_table} WHERE apid=$apid");
	}

	/**
	 * 回收站
	 * @param $apid
	 * @return bool
	 */
	function recycle($apid) {
		if (is_array($apid)) {
			foreach ($apid as $v) {
				$this->recycle($v);
			}
		} else {
			$this->_db->query(Sql::update($this->_table, "status=0", "apid=$apid"));
			$this->delete($apid, false);
			return true;
		}
		return true;
	}

	/**
	 * 还原为默认状态
	 * @param $apid
	 * @return bool
	 */
	function restore($apid) {
		if (is_array($apid)) {
			foreach ($apid as $v) {
				$this->restore($v);
			}
		} else {
			$this->_db->query(Sql::update($this->_table, "status=3", "apid=$apid"));
			return true;
		}
		return true;
	}

	/**
	 * 删除条目
	 * @param      $apid
	 */
	function delete($apid) {
		if (is_array($apid)) {
			foreach ($apid as $v) {
				$this->delete($v);
			}
		} else {
			$this->_apid = $apid;
			$r           = $this->getOne();;
			Picture::deleteAll($r['imgurl']);
			$this->_db->query(Sql::delete($this->_table, "apid=$apid"));
		}
	}

	/**
	 * 通过审核
	 * @param $apid
	 * @return bool
	 */
	function check($apid) {
		global $_username, $DT_TIME;
		if (is_array($apid)) {
			foreach ($apid as $v) {
				$this->check($v);
			}
		} else {
			$this->_apid = $apid;
			$this->_db->query(Sql::update($this->_table, "status=3,hits=hits+1,editor='$_username',edittime=$DT_TIME", "apid=$apid"));
			return true;
		}
		return true;
	}

	/**
	 * 未通过审核
	 * @param $apid
	 * @return bool
	 */
	function reject($apid) {
		global $_username;
		if (is_array($apid)) {
			foreach ($apid as $v) {
				$this->reject($v);
			}
		} else {
			$this->_db->query(Sql::update($this->_table, "status=1,editor='$_username'", "apid=$apid"));
			return true;
		}
		return true;
	}

	/**
	 * 全部删除回收站里的内容
	 * @param string $condition
	 */
	function clear($condition = 'status=0') {
		$result = $this->_db->query("SELECT apid FROM {$this->_table} WHERE $condition ");
		while ($r = $this->_db->fetchArray($result)) {
			$this->delete($r['apid']);
		}
	}

	/**
	 * 级别设置
	 * @param $apid
	 * @param $level
	 */
	function level($apid, $level) {
		$apids = is_array($apid) ? implode(',', $apid) : $apid;
		$this->_db->query(Sql::update($this->_table, "level=$level", "apid IN ($apids)"));
	}
}